package com.kevin.springcloud.controller;

import com.kevin.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class DeptConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    // private static final String REST_URL_PREFIX = "http://localhost:8001";
    /**
     * 实现负载均衡的时候，这里的地址应该是一个变量，通过服务名来进行访问
     */
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/dept/get/{id}")
    public Dept get(@PathVariable(value = "id") Long id) {

        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {

        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }


    @GetMapping("/dept/getAll")
    public List<Dept> getAll() {

        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/getAll", List.class);
    }


}
